<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiLuntan extends ApiCommon
{
    public function initialize(){
		parent::initialize();
		$bset = Db::name('luntan_sysset')->where('aid',aid)->find();
		if($bset['status'] == 0){
			die(jsonEncode(['status'=>-3,'url'=>'/pages/index/index']));
		}
	}
	public function index(){
		$sysset = Db::name('luntan_sysset')->where('aid',aid)->find();
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['status','=',1];
		$pernum = 20;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$datalist = Db::name('luntan')->where($where)->page($pagenum,$pernum)->order('is_top desc,id desc')->select()->toArray();
		if(!$datalist) $datalist = array();
		foreach($datalist as $k=>$v){
			$datalist[$k]['plcount'] = Db::name('luntan_pinglun')->where('sid',$v['id'])->where('status',1)->count();
			//是否点赞
			$zanlog = Db::name('luntan_zanlog')->where('sid',$v['id'])->where('mid',mid)->find();
			if($zanlog){
				$datalist[$k]['iszan'] = 1;
			}else{
				$datalist[$k]['iszan'] = 0;
			}
			$datalist[$k]['showtime'] = $this->getshowtime($v['createtime']);
			if($v['pics']){
				$datalist[$k]['pics'] = explode(',',$v['pics']);
			}
		}
		if($pagenum!=1){
			return $this->json(['status'=>1,'datalist'=>$datalist]);
		}
		$clist = Db::name('luntan_category')->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['datalist'] = $datalist;
		$rdata['sysset'] = $sysset;
		$rdata['title'] = $sysset['title'];
		$rdata['clist'] = $clist;
		return $this->json($rdata);
	}

	public function ltlist(){
		$sysset = Db::name('luntan_sysset')->where('aid',aid)->find();
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['status','=',1];
		if(input('param.keyword')){
			$where[] = ['content|nickname','like','%'.input('param.keyword').'%'];
		}
		$title = $sysset['title'];
		$banner = '';
		if(input('param.cid')){
			$where[] = ['cid','=',input('param.cid')];
			$cdata = Db::name('luntan_category')->where('aid',aid)->where('id',input('param.cid'))->find();
			$title = $cdata['name'];
			$banner = $cdata['banner'];
		}
		$pernum = 20;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$datalist = Db::name('luntan')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = array();
		foreach($datalist as $k=>$v){
			$datalist[$k]['plcount'] = Db::name('luntan_pinglun')->where('sid',$v['id'])->where('status',1)->count();
			//是否点赞
			$zanlog = Db::name('luntan_zanlog')->where('sid',$v['id'])->where('mid',mid)->find();
			if($zanlog){
				$datalist[$k]['iszan'] = 1;
			}else{
				$datalist[$k]['iszan'] = 0;
			}
			$datalist[$k]['showtime'] = $this->getshowtime($v['createtime']);
			if($v['pics']){
				$datalist[$k]['pics'] = explode(',',$v['pics']);
			}
		}
		if($pagenum!=1){
			return $this->json(['status'=>1,'datalist'=>$datalist]);
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['cid'] = input('param.cid');
		$rdata['title'] = $title;
		$rdata['banner'] = $banner;
		$rdata['datalist'] = $datalist;
		$rdata['sysset'] = $sysset;
		return $this->json($rdata);
	}
	
	public function fatie(){
		$this->checklogin();
		$sysset = Db::name('luntan_sysset')->where('aid',aid)->find();
		$sendtj = explode(',',$sysset['sendtj']);
		if(!in_array('-1',$sendtj) && !in_array($this->member['levelid'],$sendtj)){ //不是所有人
			if(in_array('0',$sendtj)){ //关注用户才能领
				if($this->member['subscribe']!=1){
					$appinfo = getappinfo(aid,'mp');
					return $this->fetch('guanzhu',['img'=>$appinfo['qrcode'],'msg'=>'请先关注'.$appinfo['nickname'].'公众号']);
				}
			}else{
				return $this->json(['status'=>0,'msg'=>'您没有发帖权限']);
			}
		}

		if(request()->isPost()){
			$title = input('post.title');
			$content = input('post.content');
			$pics = input('post.pics');
			$video = input('post.video');
			$cid = input('post.cid');
			$data = [];
			$data['aid'] = aid;
			$data['cid'] = $cid;
			$data['mid'] = mid;
			$data['content'] = $content;
			$data['pics'] = $pics;
			$data['video'] = $video;
			$data['createtime'] = time();
			$data['headimg'] = $this->member['headimg'];
			$data['nickname'] = $this->member['nickname'];
			if($sysset['sendcheck']==1){//需要审核
				$data['status'] = 0;
				$msg = '提交成功，请等待审核';
			}else{
				$data['status'] = 1;
				$msg = '发布成功';
			}
			$id = Db::name('luntan')->insertGetId($data);
			return $this->json(['status'=>1,'msg'=>$msg]);
		}
		$clist = Db::name('luntan_category')->where('aid',aid)->where('pid',0)->where('status',1)->order('sort desc,id')->select()->toArray();
		$rdata = [];
		$rdata['clist'] = $clist;
		return $this->json($rdata);
	}
	public function detail(){
		$id = input('param.id/d');
		$detail = Db::name('luntan')->where('id',$id)->where('status',1)->find();
		if(!$detail) return $this->json(['status'=>0,'msg'=>'帖子已删除']);
		Db::name('luntan')->where('id',input('param.id/d'))->where('aid',aid)->inc('readcount')->update();
		$detail['readcount']++;
		$detail['showtime'] = $this->getshowtime($detail['createtime']);
		if($detail['pics']){
			$detail['pics'] = explode(',',$detail['pics']);
		}

		//评论
		$pernum = 20;
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$datalist = Db::name('luntan_pinglun')->where('sid',$id)->where('status',1)->page($pagenum,$pernum)->order('createtime desc')->select()->toArray();
		if(!$datalist) $datalist = array();
		foreach($datalist as $k=>$v){
			$rs = Db::name('luntan_pzanlog')->where('pid',$v['id'])->where('mid',mid)->find();
			if($rs){
				$v['iszan'] = 1;
			}else{
				$v['iszan'] = 0;
			}
			//回复
			$replylist = Db::name('luntan_pinglun_reply')->field('id,mid,nickname,headimg,content,createtime')->where('pid',$v['id'])->where('status',1)->order('createtime')->select()->toArray();
			foreach($replylist as $k2=>$v2){
				$v2['createtime'] = $this->getshowtime($v2['createtime']);
				$v2['content'] = getshowcontent($v2['content']);
				$replylist[$k2] = $v2;
			}
			$v['replylist'] = $replylist;
			$v['content'] = nl2br(getshowcontent($v['content']));
			$v['createtime'] = $this->getshowtime($v['createtime']);
			$datalist[$k] = $v;
		}
		$plcount = Db::name('luntan_pinglun')->where('sid',$id)->where('status',1)->count();
		//是否点赞
		$zanlog = Db::name('luntan_zanlog')->where('sid',$detail['id'])->where('mid',mid)->find();
		if($zanlog){
			$iszan = 1;
		}else{
			$iszan = 0;
		}

		$rdata = [];
		$rdata['mid'] = mid;
		$rdata['datalist'] = $datalist;
		$rdata['plcount'] = $plcount;
		$rdata['iszan'] = $iszan;
		$rdata['status'] = 1;
		$rdata['detail'] = $detail;
		return $this->json($rdata);
	}
	//点赞
	public function zan(){
		$this->checklogin();
		$id = input('post.id/d');
		$detail = Db::name('luntan')->where('id',$id)->find();
		$zanlog = Db::name('luntan_zanlog')->where('sid',$id)->where('mid',mid)->find();
		if($zanlog){
			Db::name('luntan_zanlog')->where('sid',$id)->where('mid',mid)->delete();
			$type = 0;
			Db::name('luntan')->where('id',$id)->dec('zan')->update();
		}else{
			$data = [];
			$data['aid'] = aid;
			$data['sid'] = $id;
			$data['mid'] = mid;
			$data['createtime'] = time();
			Db::name('luntan_zanlog')->insert($data);
			$type = 1;
			Db::name('luntan')->where('id',$id)->inc('zan')->update();
		}
		$zancount = Db::name('luntan')->where('id',$id)->value('zan');
		return $this->json(['status'=>1,'type'=>$type,'zancount'=>$zancount]);
	}
	//评论
	public function subpinglun(){
		$this->checklogin();
		$id = input('param.id/d');
		$type = input('param.type/d');
		$hfid = input('param.hfid/d');
		$content = trim(input('param.content'));
		if(!$id){
			return $this->json(['status'=>0,'msg'=>'参数错误']);
		}
		$detail = Db::name('luntan')->where('id',$id)->where('status',1)->find();
		//if($detail['canpl']==0) return $this->json(['status'=>0,'msg'=>'评论功能未开启']);
		//if($hfid && $detail['canplrp']==0) return $this->json(['status'=>0,'msg'=>'评论回复功能未开启']);

		if($content==''){
			return $this->json(['status'=>1,'msg'=>'请输入评论内容']);
		}
		$sysset = Db::name('luntan_sysset')->where('aid',aid)->find();
		if($type==0){
			$data = [];
			$data['aid'] = aid;
			$data['mid'] = mid;
			$data['sid'] = $id;
			$data['headimg'] = $this->member['headimg'];
			$data['nickname'] = $this->member['nickname'];
			$data['content'] = $content;
			$data['createtime'] = time();
			if($sysset['pingluncheck']==1){
				$data['status'] = 0;
				$msg = '提交成功，请等待审核';
			}else{
				$data['status'] = 1;
				$msg = '发表评论成功';
			}
			Db::name('luntan_pinglun')->insert($data);
            }else{
			$data = [];
			$data['aid'] = aid;
			$data['mid'] = mid;
			$data['sid'] = $id;
			$data['pid'] = $hfid;
			$data['headimg'] = $this->member['headimg'];
			$data['nickname'] = $this->member['nickname'];
			$data['content'] = $content;
			$data['createtime'] = time();
			if($sysset['pingluncheck']==1){
				$data['status'] = 0;
				$msg = '提交成功，请等待审核';
			}else{
				$data['status'] = 1;
				$msg = '发表评论成功';
			}
			Db::name('luntan_pinglun_reply')->insert($data);
		}
		return $this->json(['status'=>1,'msg'=>$msg,'url'=>true]);
	}
	//评论点赞
	public function pzan(){
		$this->checklogin();
		$id = input('post.id/d');
		$pinglun = Db::name('luntan_pinglun')->where('id',$id)->find();
		$zanlog = Db::name('luntan_pzanlog')->where('pid',$id)->where('mid',mid)->find();
		if($zanlog){
			Db::name('luntan_pzanlog')->where('pid',$id)->where('mid',mid)->delete();
			$type = 0;
			Db::name('luntan_pinglun')->where('id',$id)->dec('zan')->update();
		}else{
			$data = [];
			$data['aid'] = aid;
			$data['pid'] = $id;
			$data['mid'] = mid;
			$data['createtime'] = time();
			Db::name('luntan_pzanlog')->insert($data);
			$type = 1;
			Db::name('luntan_pinglun')->where('id',$id)->inc('zan')->update();
		}
		$zancount = Db::name('luntan_pinglun')->where('id',$id)->value('zan');
		return $this->json(['status'=>1,'type'=>$type,'zancount'=>$zancount]);
	}
	//删除
	public function deltie(){
		$this->checklogin();
		$id = input('param.id/d');
		$detail = Db::name('luntan')->where('aid',aid)->where('id',$id)->find();
		if($detail['mid']!=mid){
			return $this->json(['status'=>0,'msg'=>'无权限操作']);
		}
		Db::name('luntan')->where('aid',aid)->where('id',$id)->delete();
		Db::name('luntan_pinglun')->where('aid',aid)->where('sid',$id)->delete();
		Db::name('luntan_pinglun_reply')->where('aid',aid)->where('sid',$id)->delete();
		return $this->json(['status'=>1,'msg'=>'删除成功']);
	}
	//删除评论
	public function delpinglun(){
		$this->checklogin();
		$id = input('param.id/d');
		$pinglun = Db::name('luntan_pinglun')->where('aid',aid)->where('id',$id)->find();
		if($pinglun['mid']!=mid){
			return $this->json(['status'=>0,'msg'=>'无权限操作']);
		}
		Db::name('luntan_pinglun')->where('aid',aid)->where('id',$id)->delete();
		Db::name('luntan_pinglun_reply')->where('aid',aid)->where('pid',$id)->delete();
		return $this->json(['status'=>1,'msg'=>'删除成功','url'=>true]);
	}
	//删除回复
	public function delplreply(){
		$this->checklogin();
		$id = input('param.id/d');
		$plreply = Db::name('luntan_pinglun_reply')->where('aid',aid)->where('id',$id)->find();
		if($plreply['mid']!=mid){
			return $this->json(['status'=>0,'msg'=>'无权限操作']);
		}
		Db::name('luntan_pinglun_reply')->where('aid',aid)->where('id',$id)->delete();
		return $this->json(['status'=>1,'msg'=>'删除成功','url'=>true]);
	}

	
	//显示时间
	private function getshowtime($time){
		if(time() - $time < 60){
			return '刚刚';
		}elseif(time() - $time < 3600){
			$minite = ceil((time() - $time)/60);
			return $minite.'分钟前';
		}elseif(date('Ymd')==date('Ymd',$time)){
			return date('H:i',$time);
		}elseif(time()-$time<86400){
			return '昨天 '.date('H:i',$time);
		}elseif(date('Y')==date('Y',$time)){
			return date('m-d H:i',$time);
		}else{
			return date('Y-m-d H:i',$time);
		}
	}

	//个人发帖记录
	public function fatielog(){
		if(request()->isPost()){
			$pagenum = input('post.pagenum');
			if(!$pagenum) $pagenum = 1;
			$pernum = 20;
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['mid','=',mid];
			$where[] = ['status','=',1];
			$datalist = Db::name('luntan')
				->where($where)
				->field('id,content,pics,createtime')
				->page($pagenum,$pernum)
				->order('is_top desc,id desc')
				->select()
				->toArray();
			if(!$datalist){
				$datalist = [];
			}else{
				foreach($datalist as &$v){
					$v['pic'] = '';
					if($v['pics']){
						$v['pics'] = explode(',',$v['pics']);
						$v['pic']  = $v['pics'][0];
					}
					$v['createtime'] = date("Y-m-d H:i:s",$v['createtime']);
				}
				unset($v);
			}
			return $this->json(['status'=>1,'data'=>$datalist] );
		}
	}

	//个人关注记录
	public function focuslog(){
		if(request()->isPost()){
			$pagenum = input('post.pagenum');
			if(!$pagenum) $pagenum = 1;
			$pernum = 20;
			$datalist = Db::name('luntan_zanlog')
				->alias('lz')
				->join('luntan l','l.id = lz.sid')
				->where('lz.mid',mid)
				->where('lz.aid',aid)
				->page($pagenum,$pernum)
				->order('lz.id desc')
				->field('lz.id,lz.sid,l.content,l.pics,lz.createtime')
				->select()
				->toArray();
			if(!$datalist){
				$datalist = [];
			}else{
				foreach($datalist as &$v){
					$v['pic'] = '';
					if($v['pics']){
						$v['pics'] = explode(',',$v['pics']);
						$v['pic']  = $v['pics'][0];
					}
					$v['createtime'] = date("Y-m-d H:i:s",$v['createtime']);
				}
				unset($v);
			}
			return $this->json(['status'=>1,'data'=>$datalist] );
		}
	}


}